package com.volunteer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.volunteer.common.Result;
import com.volunteer.entity.User;
import com.volunteer.entity.VolunteerProfile;
import com.volunteer.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 分页查询用户列表（管理员）
     */
    @GetMapping
    @PreAuthorize("hasRole('ADMIN')")
    public Result<IPage<User>> getUserList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String role) {
        IPage<User> result = userService.getUserList(page, size, keyword, role);
        return Result.success(result);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public Result<Void> updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        user.setPassword(null); // 防止通过此接口修改密码
        userService.updateUser(user);
        return Result.success("更新成功", null);
    }

    /**
     * 修改密码
     */
    @PutMapping("/{id}/password")
    public Result<Void> changePassword(
            @PathVariable Long id,
            @RequestBody Map<String, String> request) {
        String oldPassword = request.get("oldPassword");
        String newPassword = request.get("newPassword");
        userService.changePassword(id, oldPassword, newPassword);
        return Result.success("密码修改成功", null);
    }

    /**
     * 启用/禁用用户（管理员）
     */
    @PutMapping("/{id}/toggle-status")
    @PreAuthorize("hasRole('ADMIN')")
    public Result<Void> toggleUserStatus(@PathVariable Long id) {
        userService.toggleUserStatus(id);
        return Result.success("操作成功", null);
    }

    /**
     * 获取志愿者信息
     */
    @GetMapping("/{userId}/profile")
    public Result<VolunteerProfile> getVolunteerProfile(@PathVariable Long userId) {
        VolunteerProfile profile = userService.getVolunteerProfile(userId);
        return Result.success(profile);
    }

    /**
     * 更新志愿者信息
     */
    @PutMapping("/{userId}/profile")
    public Result<Void> updateVolunteerProfile(
            @PathVariable Long userId,
            @RequestBody VolunteerProfile profile) {
        profile.setUserId(userId);
        userService.updateVolunteerProfile(profile);
        return Result.success("更新成功", null);
    }

    /**
     * 获取志愿者列表
     */
    @GetMapping("/volunteers")
    public Result<IPage<VolunteerProfile>> getVolunteerList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        IPage<VolunteerProfile> result = userService.getVolunteerList(page, size);
        return Result.success(result);
    }
}

